﻿/**
 * LineEmitter
 *
 * @author		Allen Chou
 * @version 	1.1.1 (last update: Aug 26 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

/*
 * Copyright 2008 (C) Allen Chou (cjcat2266@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package idv.cjcat.display.particles {
	import flash.display.*;
	import flash.events.*;
	import idv.cjcat.display.particles.EmitterConfigMask;
	
	import idv.cjcat.*;
	import idv.cjcat.display.*;
	import idv.cjcat.geom.*;
	
	public class LineEmitter extends AbstractEmitter {
		
		public var centerBiasIter:uint = 0;
		
		private var _lengthR:Number = 50;
		private var _lengthL:Number = 50;
		
		public function LineEmitter(displayClass:Class = null, container:DisplayObjectContainer = null) {
			super(displayClass, container);
		}
		
		private var slopeAng:Number;
		override public function spawn():DisplayObject {
			var temp:DisplayObject = new displayClass();
			slopeAng = Math.atan2(normal.y, normal.x) + Math.PI / 2;
			var randPos:Number = CJMath.pseudoGaussian(0.5, 0.5, centerBiasIter + 1);
			temp.x = centerX + CJMath.interpolate(0, -_lengthL * Math.cos(slopeAng), 1, _lengthR * Math.cos(slopeAng), randPos);
			temp.y = centerY + CJMath.interpolate(0, -_lengthL * Math.sin(slopeAng), 1, _lengthR * Math.sin(slopeAng), randPos);
			return temp;
		}
		
		private var a:int;
		private var b:int;
		private var c:Number;
		private var tempDir:Vector2;
		private var ang:Number;
		private var normalAng:Number;
		private var arr:Array;
		public function uniBurst(count:int = 10, repetitions:int = 1, interval:Number = 1, directionVar:Boolean = true):void {
			if (count <= 0) return;
			if (repetitions <= 0) return;
			if (interval <= 0) return;
			
			for (b = 1; b <= repetitions; b++) {
				tempDir = _direction.unitVec().rotate(0, false);
				arr = new Array();
				
				if (count == 1) {
					ang = Math.atan2(tempDir.y, tempDir.x);
					arr.push( { x:centerX, y:centerY, vx:speed * Math.cos(ang), vy:speed * Math.sin(ang) } );
					
					if (bidirectional) {
						ang = Math.atan2(tempDir.y, tempDir.x);
						arr.push( { x:centerX, y:centerY, vx:-speed * Math.cos(ang), vy:-speed * Math.sin(ang) } );
					}
				} else {
					normalAng = Math.atan2(normal.y, normal.x) / Math.PI * 180;
					if (directionVar) {
						tempDir = tempDir.rotate(this.directionVar, false);
						for (a = 1; a <= count; a++) {
							c = (a - 1) / (count - 1);
							ang = Math.atan2(tempDir.y, tempDir.x);
							slopeAng = Math.atan2(normal.y, normal.x) + Math.PI / 2;
							arr.push( { x:centerX + CJMath.interpolate(0, -_lengthL * Math.cos(slopeAng), 1, _lengthR * Math.cos(slopeAng), c),
										y:centerY + CJMath.interpolate(0, -_lengthL * Math.sin(slopeAng), 1, _lengthR * Math.sin(slopeAng), c),
										vx:speed * Math.cos(ang), vy:speed * Math.sin(ang) } );
							tempDir = tempDir.rotate(-2 * this.directionVar / (count - 1), false);
						}
						
						
						if (bidirectional) {
							tempDir = _direction.unitVec().rotate(-this.directionVar, false);
							for (a = 1; a <= count; a++) {
								c = (a - 1) / (count - 1);
								ang = Math.atan2(tempDir.y, tempDir.x);
								slopeAng = Math.atan2(normal.y, normal.x) + Math.PI / 2;
								arr.push( { x:centerX + CJMath.interpolate(0, -_lengthL * Math.cos(slopeAng), 1, _lengthR * Math.cos(slopeAng), c),
											y:centerY + CJMath.interpolate(0, -_lengthL * Math.sin(slopeAng), 1, _lengthR * Math.sin(slopeAng), c),
											vx:-speed * Math.cos(ang), vy:-speed * Math.sin(ang) } );
								tempDir = tempDir.rotate(2 * this.directionVar / (count - 1), false);
							}
						}
					} else {
						for (a = 1; a <= count; a++) {
							c = (a - 1) / (count - 1);
							ang = Math.atan2(tempDir.y, tempDir.x);
							slopeAng = Math.atan2(normal.y, normal.x) + Math.PI / 2;
							arr.push( { x:centerX + CJMath.interpolate(0, -_lengthL * Math.cos(slopeAng), 1, _lengthR * Math.cos(slopeAng), c),
										y:centerY + CJMath.interpolate(0, -_lengthL * Math.sin(slopeAng), 1, _lengthR * Math.sin(slopeAng), c),
										vx:speed * Math.cos(ang), vy:speed * Math.sin(ang) } );
						}
						
						if (bidirectional) {
							
						}
					}
				}
				
				_uniBurstQueue.push({life: interval * (b - 1), array:arr});
			}
			
		}
		
		override public function getConfig(mask:EmitterConfigMask = null):Object {
			if (mask == null) mask = new EmitterConfigMask();
			var obj:Object = super.getConfig(mask);
			obj.length = this.length;
			
			return obj;
		}
		
		//accessors
		//------------------------------------------------------------------------------------------------
		
		override public function set showAuxShape(value:Boolean):void {
			if (_auxShape != null) removeChild(_auxShape);
			_showAuxShape = value;
			if (value) {
				_auxShape = new Shape();
				CJDisplay.setLineStyle(_auxShape, auxShapeLineStyle);
				_auxShape.graphics.moveTo(-_lengthL, 0);
				_auxShape.graphics.lineTo(_lengthR, 0);
				
				_auxShape.graphics.moveTo(0, 0);
				_auxShape.graphics.lineTo(0, -30);
				_auxShape.graphics.lineTo( 10, -15);
				_auxShape.graphics.lineTo(-10, -15);
				_auxShape.graphics.lineTo(0, -30);
				updateAuxShape();
				addChild(_auxShape);
			} else {
				if (_auxShape != null) removeChild(_auxShape);
				_auxShape = null;
			}
		}
		
		override public function updateAuxShape(e:Event = null):void {
			if (_auxShape != null) {
				_auxShape.rotation = 90 + (Math.atan2(_normal.y, _normal.x) / Math.PI * 180);
				_auxShape.x = centerX;
				_auxShape.y = centerY;
			}
		}
		
		public function get length():Number { return _lengthL + _lengthR; }
		public function set length(value:Number):void {
			_lengthL = _lengthR = value / 2;
		}
		
		public function get lengthL():Number { return _lengthL; }
		public function set lengthL(value:Number):void {
			_lengthL = CJMath.clamp(value, 0, Number.MAX_VALUE);
			if (showAuxShape) showAuxShape = true;
		}
		
		public function get lengthR():Number { return _lengthR; }
		public function set lengthR(value:Number):void {
			_lengthR = CJMath.clamp(value, 0, Number.MAX_VALUE);
			if (showAuxShape) showAuxShape = true;
		}
		
		//end of accessors
		//------------------------------------------------------------------------------------------------
		
	}
}